/**
  ******************************************************************************
  * @file    bsp_uart_ex.c
  * @author  Iron
  * @date    2022-01-27
  * @version v1.0
  * @brief   bsp_uart_ex c file
  */

/* Private includes ----------------------------------------------------------*/
#include "bsp_uart_ex.h"

/* DEBUG LOG */
//#include "debug_log.h"
//#define LOG_LOCAL_LEVEL     DBG_LOG_DEBUG
//DBG_LOG_TAG("BSP_UART_EX");

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static rx_completed_cb_function_t usart1_rx_cb = NULL;
static rx_completed_cb_function_t usart2_rx_cb = NULL;

/* Private function prototypes -----------------------------------------------*/

/*----------------------------------------------------------------------------*/
void bsp_uart_ex_add_rx_completed_irq_cb(bsp_uart_id_t uart_id, uint16_t size)
{
    switch (uart_id)
    {
    case BSP_UART0:
        if (usart1_rx_cb)
        {
            usart1_rx_cb(uart_id, size);
        }
        break;

    case BSP_UART1:
        if (usart2_rx_cb)
        {
            usart2_rx_cb(uart_id, size);
        }
        break;

    default:
        break;
    }
}

/*----------------------------------------------------------------------------*/
int32_t bsp_uart_ex_add_rx_completed_cb(bsp_uart_id_t uart_id, rx_completed_cb_function_t rx_irq_cb)
{
    switch (uart_id)
    {
    case BSP_UART0:
        usart1_rx_cb = rx_irq_cb;
        break;

    case BSP_UART1:
        usart2_rx_cb = rx_irq_cb;
        break;
    default:
        break;
    }

    return BSP_UART_OK;
}

/*----------------------------------------------------------------------------*/
uint32_t bsp_uart_ex_get_timeout(uint32_t bps, int32_t len)
{
    uint32_t timeout = 10; // ms

    timeout += (len * (1 + 8 + 1) * 1000 / bps);

    return timeout;
}

/**
  * @}
  */

/******************* (C)COPYRIGHT 2021 ***** END OF FILE *********************/
